#packages
library(readxl)
library(plyr)
library(dplyr)
library(lme4)
library(ggplot2)
library(ggmosaic)
library(showtext)

font_add(family = "Sabon", regular = "/Users/xyehla/Library/Group Containers/UBF8T346G9.Office/FontCache/4/CloudFonts/Sabon Next LT/30873337876.ttf")
showtext_auto()

clean_data <- read_xlsx("/Users/xyehla/Library/Mobile Documents/com~apple~CloudDocs/GU WORK/Ethical justifications Policy Acceptance/Submissions/data for osf/clean.xlsx")
setwd ("/Users/xyehla/Library/Mobile Documents/com~apple~CloudDocs/GU WORK/Ethical justifications Policy Acceptance/Submissions/R graphs")

######graph 1: means and ci
group <- clean_data |>
  dplyr::select(group, treat, case, dv) |>
ddply(c("group"), summarise,
                N    = sum(!is.na(dv)),
                mean = mean(dv, na.rm=TRUE),
                sd   = sd(dv, na.rm=TRUE),
                se   = sd / sqrt(N),
                case=unique(case),
                treat=unique(treat)) |> 
  mutate(
    treat=case_when(treat==1~"Control", treat==2~"Anthropocentric", treat==3~"Non-Anthropocentric"),
    
  )
         

graph1 <- ggplot(group, aes(x=case, y=mean, colour = as.factor(treat))) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.03, size=1) +
  geom_point(size=1.5)+
  geom_line(aes(linetype = treat), size=1)+
  scale_linetype_manual(breaks = c("Control", "Anthropocentric", "Non-Anthropocentric"), values=c("solid","solid", "solid"))+
  ylab("Mean")+
  xlab("Policy Case")+
  ggtitle("Policy Acceptability, Mean and CI", subtitle="What is your opinion of using the state budget to fund a bill of this kind in your state?") +
  guides(color=guide_legend(title="Policy Justification"), linetype="none")+
  scale_x_continuous(breaks=1:2, labels = c("Infrastructure", "Conservation")) +
  scale_y_continuous(limits = c(5.2, 6), breaks = seq(5.2, 6, by=.2)) +
  theme_bw() +
  scale_colour_grey()+
  theme(
    text = element_text(family = "Sabon", size = 15),  # Set font family and size
    axis.title = element_text(size = 20, color = "black"),  # Black axis titles
    axis.text = element_text(size = 16, color = "black"),   # Black axis text
    panel.grid.major = element_line(color = "black", linetype = "dotted"),  # Dotted grid lines
    panel.grid.minor = element_blank(),  # Remove minor grid lines
    panel.background = element_rect(fill = "white"),  # White background
    plot.background = element_rect(fill = "white")    # White plot background
  )

setwd ("/Users/xyehla/Dropbox/Ethical justifications Policy Acceptance/Submissions/R graphs")
tiff("YEHLE1.tiff", width = 10, height = 6, units = "in", res = 600)
print(graph1)
dev.off()




#####graph 2: perceived and preferred justification

graph2df <- data.frame(
  treat=c(1,2,3,1,2,3),
  type=c("Perceived Justification", "Perceived Justification", "Perceived Justification", "Preferred Justificaton", "Preferred Justificaton","Preferred Justificaton"), 
  margins=c(3.3785, 2.963164, 3.928303, 4.31143,4.074874,4.483618),
  se=c(.0675638,.0658891,.0677561, .0608532,.0594566,.0610268)
)

graph2<-ggplot(graph2df, aes(x=treat, y=margins, colour=as.factor (type)))+
  geom_ribbon(aes(ymin=4.0, ymax = 5.0), alpha = 0.08, fill="gray")+
  geom_ribbon(aes(ymin=2.5, ymax = 4.0), alpha = 0.2, fill="gray")+
  geom_errorbar(aes(ymin=margins-se, ymax=margins+se), width=.03, size=1) +
  geom_point(size=1.5)+
  geom_line(size=1)+
  ylab("Linear Predicted Margins")+
  xlab("Policy Justification")+
  ggtitle("Perceived and Preferred Justification", subtitle="What do you think the government is/should be most concerned for...?") +
  guides(color=guide_legend(title="Outcome Variable"))+
  scale_x_continuous(breaks=1:3, labels = c("Control", "Anthropocentric", "Non-Anthropocentric"))+ 
  scale_y_continuous(limits = c(2.5, 5.0), breaks = seq(2.5, 5.0, by=.5)) +
  annotate("text", x=1.3, y=2.7, label="People and Society") +
  annotate("text", x=1.3, y=4.75, label="Wildlife and Ecosystems") +
  theme_bw()+
  scale_colour_grey()+
  theme(
    text = element_text(family = "Sabon", size = 15),  # Set font family and size
    axis.title = element_text(size = 20, color = "black"),  # Black axis titles
    axis.text = element_text(size = 16, color = "black"),   # Black axis text
    panel.grid.major = element_line(color = "black", linetype = "dotted"),  # Dotted grid lines
    panel.grid.minor = element_blank(),  # Remove minor grid lines
    panel.background = element_rect(fill = "white"),  # White background
    plot.background = element_rect(fill = "white")    # White plot background
  )

tiff("YEHLE2.tiff", width = 10, height = 6, units = "in", res = 600)
print(graph2)
dev.off()


graph3df <- data.frame(
  case=c(1,1,2,2),
  pol=c("Republican", "Democrat", "Republican", "Democrat"),
  margins=c(5.17218, 5.644317,5.232652,6.09419),
  se=c(.0804,.0529,.0809,.052013)
)

graph3<-ggplot(graph3df, aes(x=case, y=margins, colour=as.factor (pol)))+
  geom_errorbar(aes(ymin=margins-se, ymax=margins+se), width=.03, size=1)+ 
  geom_point(size=1.5)+
  geom_line(size=1) +
  ylab("Linear Predicted Policy Acceptability")+
  xlab("Policy Case")+
  ggtitle("Policy Case and Political Orientation Interaction", subtitle="What is your opinion of using the state budget to fund a bill of this kind in your state?") +
  guides(color=guide_legend(title="Political Orientation"))+
  scale_x_continuous(breaks=1:2, labels = c("Infrastructure", "Conservation")) +
  scale_y_continuous(limits = c(5.0, 6.2), breaks = seq(5.0, 6.2, by=.2)) +
  theme_bw()+
  scale_colour_grey()+
  theme(
    text = element_text(family = "Sabon", size = 15),  # Set font family and size
    axis.title = element_text(size = 20, color = "black"),  # Black axis titles
    axis.text = element_text(size = 16, color = "black"),   # Black axis text
    panel.grid.major = element_line(color = "black", linetype = "dotted"),  # Dotted grid lines
    panel.grid.minor = element_blank(),  # Remove minor grid lines
    panel.background = element_rect(fill = "white"),  # White background
    plot.background = element_rect(fill = "white")    # White plot background
  )

tiff("YEHLE3.tiff", width = 10, height = 6, units = "in", res = 600)
print(graph3)
dev.off()
